﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using agent02.com.somee.g02_transport;
using agent02.Model;

namespace agent02.Controllers
{
    public class TransportController : Controller
    {
        Service ws = new Service();
        //
        // GET: /Transport/
        public ActionResult Index()
        {
            
            return View();
        }

        //
        // GET: /Transport/SearchService
        public ActionResult SearchService()
        {
            TransportSearchModel t = new TransportSearchModel();
            return View(t);
        }

        [HttpPost]
        public ActionResult SearchService(TransportSearchModel t)
        {
            return RedirectToAction("CarSearchResult", new { ServiceName = t.serviceName });
        }

        //
        // GET: /Transport/SearchSeat
        public ActionResult SearchSeat()
        {
            TransportSearchModel t = new TransportSearchModel();
            return View(t);
        }

        [HttpPost]
        public ActionResult SearchSeat(TransportSearchModel t)
        {
            return RedirectToAction("CarSearchResult", new { SeatNumber = t.seatNum });
        }

        //
        // GET: /Transport/CarSearchResult
        public ActionResult CarSearchResult(string ServiceName, string SeatNumber)
        {
            TransportConverterModel tc = new TransportConverterModel();
            IEnumerable<XE> xe;

            if ((ServiceName != null) && (SeatNumber == null))
            {
                DataSet ds = ws.TimXeTheoDichVu(ServiceName);
                if (ds != null)
                {
                    xe = tc.ConvertToXE(ds.Tables[0]);
                    return View(xe);
                }
                else
                {
                    return RedirectToAction("NoResult");
                }
            }
            else if ((ServiceName == null) && (SeatNumber != null))
            {
                DataSet ds = ws.TimXeTheoSoCho(SeatNumber);
                if (ds != null)
                {
                    xe = tc.ConvertToXE(ds.Tables[0]);
                    return View(xe);
                }
                else
                {
                    return RedirectToAction("NoResult");
                }
            }
            // wrong input
            return RedirectToAction("NoResult");
        }

        //
        // GET: /Transport/DayPrice
        public ActionResult DayPrice()
        {
            TransportSearchModel t = new TransportSearchModel();
            return View(t);
        }

        [HttpPost]
        public ActionResult DayPrice(TransportSearchModel t)
        {
            return RedirectToAction("DayPriceResult", new { id = t.serviceName });
        }

        //
        // GET: /Transport/DayPriceResult
        public ActionResult DayPriceResult(string id)
        {
            TransportConverterModel tc = new TransportConverterModel();
            DataSet ds = ws.BangGiaTrongNgay(id);
            if (ds != null)
            {
                IEnumerable<BANG_GIA_NGAY> bangGiaNgay = tc.ConvertToBANG_GIA_NGAY(ds.Tables[0]);
                return View(bangGiaNgay);
            }
            else
            {
                return RedirectToAction("NoResult");
            }
        }

        //
        // GET: /Transport/CountryPrice
        public ActionResult CountryPrice()
        {
            TransportSearchModel t = new TransportSearchModel();
            return View(t);
        }

        [HttpPost]
        public ActionResult CountryPrice(TransportSearchModel t)
        {
            return RedirectToAction("CountryPriceResult", new { id = t.country });
        }

        //
        // GET: /Transport/CountryPriceResult
        public ActionResult CountryPriceResult(string id)
        {
            TransportConverterModel tc = new TransportConverterModel();
            DataSet ds = ws.BangGiaTheoQuocGia(id);
            if (ds != null)
            {
                IEnumerable<BANG_GIA_MIEN> bangGiaMien = tc.ConvertToBANG_GIA_MIEN(ds.Tables[0]);
                return View(bangGiaMien);
            }
            else
            {
                return RedirectToAction("NoResult");
            }
        }

        //
        // GET: /Transport/NoResult
        public ActionResult NoResult()
        {
            return View();
        }

    }
}
